import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

public class Gui {

	public JFrame frame;
	public JTextField tfBet;
	private JTextField tfRaise;
	private JTextField tfPort;
	public JTextField tfBankRoll;
	private JTextField tfHost;
	public JTextArea taMsg;
	public JButton bCard1;
	public JButton bCard2;
	public JButton bCard3;
	public JButton bCard4;
	SocketClient socket;
	String text;
	String lastMsg;
	String[] msgTable;
	GuiReadThread grt;

	public static void main(String[] args) 
	{
		EventQueue.invokeLater(new Runnable() 
		{
			public void run() 
			{
				try 
				{
					Gui window = new Gui();
					window.frame.setVisible(true);						
				} 
				catch (Exception e) 
				{
					e.printStackTrace();
				}			
			}
		});
	}

	public Gui() 
	{
		initialize();
	}
	private void initialize() 
	{
		frame = new JFrame();
		socket = new SocketClient();
		grt=new GuiReadThread(socket, this);
		JButton bConnect = new JButton("Connect");
		JButton bDisconnect = new JButton("Disconnect");
		JButton bCheck = new JButton("Check");
		JButton bBet = new JButton("Bet");
		JButton bRaise = new JButton("Raise");
		JButton bCall = new JButton("Call");
		JButton bFold = new JButton("Fold");
		JButton bAllin = new JButton("All-In");
		bCard1 = new JButton("");
		bCard2 = new JButton("");
		bCard3 = new JButton("");
		bCard4 = new JButton("");
		
		tfRaise = new JTextField("0");
		taMsg = new JTextArea();
		tfBet = new JTextField("0");
		tfPort = new JTextField();
		
		frame.setResizable(false);
		frame.getContentPane().setBackground(new Color(0, 153, 0));
		frame.getContentPane().setForeground(Color.WHITE);
		frame.setBounds(100, 100, 544, 364);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
			
		bCheck.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				socket.sendMsg("CHECK");
			}
		});
		bCheck.setBounds(0, 247, 90, 35);
		frame.getContentPane().add(bCheck);
		
		bBet.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				text = tfBet.getText();
				int value = Integer.parseInt(text);
					tfBet.setText("0");
					socket.sendMsg("BET:"+value);
			}
		});
		bBet.setBounds(89, 247, 90, 35);
		frame.getContentPane().add(bBet);
		
		bRaise.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				String text = tfRaise.getText();
				int value = Integer.parseInt(text);
					tfRaise.setText("0");
					socket.sendMsg("RAISE:"+value);
			}
		});
		bRaise.setBounds(178, 247, 90, 35);
		frame.getContentPane().add(bRaise);
		
		
		bCall.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
					socket.sendMsg("CALL");			
			}
		});
		bCall.setBounds(267, 247, 95, 35);
		frame.getContentPane().add(bCall);
		
		
		bFold.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				socket.sendMsg("FOLD");
			}
		});
		bFold.setBounds(359, 247, 90, 35);
		frame.getContentPane().add(bFold);
		
		bAllin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				socket.sendMsg("ALLIN");
			}
		});
		bAllin.setBounds(449, 247, 92, 35);
		frame.getContentPane().add(bAllin);
		
		
		tfBet.setBounds(89, 216, 85, 20);
		frame.getContentPane().add(tfBet);
		tfBet.setColumns(10);
		
		
		tfRaise.setColumns(10);
		tfRaise.setBounds(183, 216, 85, 20);
		frame.getContentPane().add(tfRaise);
		
		
		bCard1.setBounds(84, 64, 90, 122);
		frame.getContentPane().add(bCard1);
		
		
		bCard2.setBounds(183, 64, 90, 122);
		frame.getContentPane().add(bCard2);
		
		
		bCard3.setBounds(283, 64, 90, 122);
		frame.getContentPane().add(bCard3);
		
		
		bCard4.setBounds(383, 64, 90, 122);
		frame.getContentPane().add(bCard4);
		
		
		bDisconnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				socket.disconnect();
				bConnect.setEnabled(true);
				tfPort.setEnabled(true);
				tfHost.setEnabled(true);
				bDisconnect.setEnabled(false);						
			}
		});
		bDisconnect.setBounds(273, 0, 89, 23);
		frame.getContentPane().add(bDisconnect);
		
		bConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				socket.connect();
				grt.start();
				if(socket.connected == true)
				{
					bConnect.setEnabled(false);
					tfPort.setEnabled(false);
					tfHost.setEnabled(false);
					bDisconnect.setEnabled(true);
					tfBankRoll.setText("500");				
				}
			}
		});
		bConnect.setBounds(0, 0, 89, 23);
		frame.getContentPane().add(bConnect);
		
		
		tfPort.setText("Port");
		tfPort.setBounds(93, 1, 86, 20);
		frame.getContentPane().add(tfPort);
		tfPort.setColumns(10);
		
		JCheckBox chCard1 = new JCheckBox("Change");
		chCard1.setBounds(89, 34, 85, 23);
		frame.getContentPane().add(chCard1);
		
		JCheckBox chCard2 = new JCheckBox("Change");
		chCard2.setBounds(195, 34, 73, 23);
		frame.getContentPane().add(chCard2);
		
		JCheckBox chCard3 = new JCheckBox("Change");
		chCard3.setBounds(290, 34, 80, 23);
		frame.getContentPane().add(chCard3);
		
		JCheckBox chCard4 = new JCheckBox("Change");
		chCard4.setBounds(390, 34, 79, 23);
		frame.getContentPane().add(chCard4);
		
		JButton bChangeCards = new JButton("Change cards");
		bChangeCards.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String text="CHANGE";
				if(chCard1.isSelected()==true)
				{
					text+=":1";
					chCard1.setSelected(false);
				}
				else
					text+=":0";
				if(chCard2.isSelected()==true)
				{
					text+=":2";
					chCard2.setSelected(false);
				}
				else
					text+=":0";
				if(chCard3.isSelected()==true)
				{
					text+=":3";
					chCard3.setSelected(false);
				}
				else
					text+=":0";
				if(chCard4.isSelected()==true)
				{
					text+=":4";
					chCard4.setSelected(false);
				}
				else
					text+=":0";
				socket.sendMsg(text);
			}
		});
		bChangeCards.setBounds(419, 0, 119, 23);
		frame.getContentPane().add(bChangeCards);
		
		JLabel lBankRoll = new JLabel("Bank Roll");
		lBankRoll.setBounds(483, 185, 54, 20);
		frame.getContentPane().add(lBankRoll);
		
		tfBankRoll = new JTextField("0");
		tfBankRoll.setEditable(false);
		tfBankRoll.setBounds(449, 205, 86, 20);
		frame.getContentPane().add(tfBankRoll);
		tfBankRoll.setColumns(10);
		
		tfHost = new JTextField();
		tfHost.setText("host");
		tfHost.setBounds(182, 1, 86, 20);
		frame.getContentPane().add(tfHost);
		tfHost.setColumns(10);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setBounds(0, 282, 541, 53);
		frame.getContentPane().add(scrollPane);
		
		taMsg.setLineWrap(true);
		taMsg.setEnabled(true);
		taMsg.setEditable(false);
		taMsg.setText("");
		scrollPane.setViewportView(taMsg);
	}
}
